﻿using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using NetBay.Core.BusinessEntities;

namespace Rio.Framework.Pages.CriteriaControls
{

    public sealed partial class IntegerIndexControl 
    {
        /// <summary>
        /// Get or set the control value
        /// </summary>
        public override string InputValue
        {
            get
            { 
                return maskedTextBoxInput.Text;
            }
            set
            {
                if (value != null)
                {
                    maskedTextBoxInput.Text = value;
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IntegerIndexControl"/> class.
        /// </summary>
        public IntegerIndexControl()
        {
            InitializeComponent();

            _originalBorderBrush = this.maskedTextBoxInput.BorderBrush as LinearGradientBrush;
        }

        /// <summary>
        /// Create an instance of the control
        /// </summary>
        /// <param name="p_oindex"><see cref="Index"/> instance</param>
        /// <param name="P_szValue">Default value</param>
        /// <param name="P_SetDefaultValue">Use default rio value</param>
        public IntegerIndexControl(Index p_oindex, string P_szValue = null, bool P_SetDefaultValue = false)
            : this()
        {
            CurrentIndex = p_oindex;
            LabelControl.Text = CurrentIndex.IndexLabel;

            //if a value is selected at the start 
            if (P_szValue != null && !string.Empty.Equals(P_szValue))
            {
                InputValue = P_szValue;
            }
            else
            {
                if (P_SetDefaultValue)
                    this.SetDefaultValue();
            }

            //Mandatory indication
            LabelMandatory.Visibility = CurrentIndex.IsMandatory ? Visibility.Visible : Visibility.Collapsed;
            FilterButton.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Display filter window with a list of values input
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void buttonInput_Click(object sender, RoutedEventArgs e)
        {
            OnFilterClick(new Events.FilterClickEventArgs(this.CurrentIndex));
        }

        /// <summary>
        /// Throw validation event
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void maskedTextBoxInput_LostFocus(object sender, RoutedEventArgs e)
        {
            OnValueValidated(EventArgs.Empty);
        }

        /// <summary>
        /// Handles the TextChanged event of the maskedTextBoxInput control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.TextChangedEventArgs"/> instance containing the event data.</param>
        private void maskedTextBoxInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!String.IsNullOrEmpty(maskedTextBoxInput.Text))
            {
                if (AcceptSpecialCharsForRegularExpression)
                {
                    Regex l_or = new Regex(@"^[0-9\%\*]*$");
                    if (!l_or.IsMatch(maskedTextBoxInput.Text))
                    {
                         maskedTextBoxInput.Text =  Regex.Replace(maskedTextBoxInput.Text, @"[^0-9\%\*]", string.Empty);
                    }
                }
                else
                {
                    Regex l_or = new Regex(@"^[0-9]*$");
                    if (!l_or.IsMatch(maskedTextBoxInput.Text))
                    {
                        maskedTextBoxInput.Text = Regex.Replace(maskedTextBoxInput.Text, @"[^0-9]", string.Empty);
                    }
                }
            }

        }

        /// <summary>
        /// Handles the Click event of the buttonDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            OnRemoveControlClick(new EventArgs());
        }

        /// <summary>
        /// Used to set border color of input control
        /// </summary>
        public override void ChangeBorderColor(Color color)
        {
            this.maskedTextBoxInput.BorderBrush = new SolidColorBrush(color);
        }

        public override void RevertBorderColor()
        {
            this.maskedTextBoxInput.BorderBrush = _originalBorderBrush;
        }

        /// <summary>
        /// Set the max lenght of the control
        /// </summary>
        public override void SetMaxLength()
        {
        }

        /// <summary>
        /// Hide mandatory label
        /// </summary>
        public override void DisableMandatoryLabel()
        {
            this.LabelMandatory.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Hides Index label name
        /// </summary>
        public override void DisableIndexLabel()
        {
            this.LabelControl.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Activate Removing feature
        /// </summary>
        public override void ActiveRemove()
        {
            this.DeleteButton.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Hides filter button
        /// </summary>
        public override void DisableFilter()
        {
            this.FilterButton.Visibility = Visibility.Collapsed;
        }
    }

}
